from PyQt4 import QtGui, QtCore
from S3D import QtHelper
from S3D.Tools.Sound.EventSearcher.UI.NameQueryWidget import Ui_NameQueryWidget
from S3D.Tools.Sound.EventSearcher.UI.SimpleQueryWidget import Ui_SimpleQueryWidget
from S3D.Tools.Sound.EventSearcher.UI.RangeQueryWidget import Ui_RangeQueryWidget
from S3D.Tools.Sound.EventSearcher.UI.ListQueryWidget import Ui_ListQueryWidget
from S3D.Tools.Sound.EventSearcher.ListSelectDialog import ListSelectDialog

class BaseQueryWidget(QtGui.QWidget):
    def __init__(self, *args):
        super(BaseQueryWidget, self).__init__()
        self.initUi(*args)

    def initUi(self, *args):
        raise NotImplementedError()

    def onChanged(self, _=None):
        self.emit(QtCore.SIGNAL("changed()"))

class SimpleQueryWidget(BaseQueryWidget):
    def initUi(self, name):
        self.ui = Ui_SimpleQueryWidget()
        self.ui.setupUi(self)

        self.ui.titleLabel.setText(name)
        QtCore.QObject.connect(self.ui.removeBtn, QtCore.SIGNAL("clicked()"), self, QtCore.SIGNAL("remove()"))

    def getData(self):
        return []

class NameQueryWidget(BaseQueryWidget):
    def initUi(self, name):
        self.ui = Ui_NameQueryWidget()
        self.ui.setupUi(self)

        self.ui.titleLabel.setText("%s:" % name)
        QtCore.QObject.connect(self.ui.removeBtn, QtCore.SIGNAL("clicked()"), self, QtCore.SIGNAL("remove()"))

        self.value = ""
        self.dataBind = QtHelper.DataBind()
        self.dataBind.bindText(self.ui.valueEdit, self, "value", self.onChanged)

    def getData(self):
        return [unicode(self.value)]

class RangeQueryWidget(BaseQueryWidget):
    def initUi(self, name, rangeFrom, rangeTo):
        self.ui = Ui_RangeQueryWidget()
        self.ui.setupUi(self)

        self.ui.titleLabel.setText("%s from" % name)
        QtCore.QObject.connect(self.ui.removeBtn, QtCore.SIGNAL("clicked()"), self, QtCore.SIGNAL("remove()"))

        self.rangeFrom = rangeFrom
        self.rangeTo   = rangeTo
        self.dataBind = QtHelper.DataBind()
        self.dataBind.bindValue(self.ui.fromValue, self, "rangeFrom", self.onChanged)
        self.dataBind.bindValue(self.ui.toValue,   self, "rangeTo",   self.onChanged)
        self.dataBind.updateValues()

    def getData(self):
        if self.rangeFrom > self.rangeTo:
            return [self.rangeTo, self.rangeFrom]

        return [self.rangeFrom, self.rangeTo]

class ListQueryWidget(BaseQueryWidget):
    def initUi(self, name, defaultValue, valuesList):
        self.ui = Ui_ListQueryWidget()
        self.ui.setupUi(self)

        QtCore.QObject.connect(self.ui.removeBtn, QtCore.SIGNAL("clicked()"), self, QtCore.SIGNAL("remove()"))

        self.isInit = True
        self.ui.titleLabel.setText("%s:" % name)
        self.value = defaultValue
        self.valuesList = valuesList

        self.ui.valueBox.addItems(valuesList)
        self.ui.valueBox.setEditText(defaultValue)
        self.isInit = False

    def onValueChanged(self, value):
        if not self.isInit:
            self.value = unicode(value)
            self.onChanged(value)

    def onBrowse(self):
        dlg = ListSelectDialog(self, self.valuesList, self.value.replace(',', ' ').split())
        if dlg.exec_() != QtGui.QDialog.Accepted:
            return

        self.ui.valueBox.setEditText(', '.join(dlg.selectedElements))

    def getData(self):
        return [self.value.replace(',', ' ').split()]

QUERY_WIDGETS = dict(
    simple      = SimpleQueryWidget,
    name        = NameQueryWidget,
    range       = RangeQueryWidget,
    list        = ListQueryWidget,
)